/**
 * @author 陈泰来
 * @version V1.0
 * @Project echo-java-base
 * @Title 转账任务处理类
 * @Description *
 * @Package concurrent.example.bank
 * @date 2021/3/11 3:35 下午
 * @Copyright:学如逆水行舟，不进则退 .
 */
package concurrent.bank;

public class TransferRunnable implements Runnable {

    private final Bank bank;
    private final int fromAccount;
    private final double maxAmount;
    private final int DELAY = 1000;

    public TransferRunnable(Bank b, int from, double max) {
        bank = b;
        fromAccount = from;
        maxAmount = max;
    }

    /**
     * @return void
     * @author chentl
     * @description 从 指定账户 随机转入 随机金额（不超过最大限制） 到 随机银行账户。
     * @date 2021/3/17 9:46 上午
     * @params []
     */
    @Override
    public void run() {
        try {
            while (true) {
                int toAccount = (int) (bank.size() * Math.random());
                double amount = maxAmount * Math.random();

                bank.transfer(fromAccount, toAccount, amount);

                Thread.sleep(DELAY);
            }
        } catch (Exception e) {

        }
    }
}
